package aviatordao

import (
	"wzgames/internal/gdao"
	"wzgames/internal/model/aviatorp"
)

type AviatorDao gdao.DaoSource

const sqlGetTopRounds = "select * from aviator_rounds where `instid`=? order by `rid` desc limit 25"

func (dao *AviatorDao) GetTopRounds(instId int64) ([]*aviatorp.AviatorRoundEntity, error) {
	rows, err := dao.Queryx(sqlGetTopRounds, instId)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var rounds []*aviatorp.AviatorRoundEntity
	for rows.Next() {
		rd := &aviatorp.AviatorRoundEntity{}
		err = rows.StructScan(rd)
		if err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		rounds = append(rounds, rd)
	}
	return rounds, err
}

const sqlGetAvtRound = "select * from aviator_rounds where `rid`=? and `instid`=? limit 1"

func (dao *AviatorDao) GetAvtRound(roundId, instId int64) (*aviatorp.AviatorRoundEntity, error) {
	r := &aviatorp.AviatorRoundEntity{}
	err := dao.Get(r, sqlGetAvtRound, roundId, instId)
	return r, err
}

const sqlCreateRound = "insert into aviator_rounds(`rid`,`instid`,`start`,`end`,`sseed`,`sseed256`,`clientseeds`,`h512`,`hhex`,`hdecimal`,`result`,`status`,`cashouts`,`count`,`runt`,`crashtime`,`created`,`updated`)" +
	" values(:rid,:instid,:start,:end,:sseed,:sseed256,:clientseeds,:h512,:hhex,:hdecimal,:result,:status,:cashouts,:count,:runt,:crashtime,:created,:updated)"

func (dao *AviatorDao) CreateRound(rd *aviatorp.AviatorRoundEntity) error {
	_, err := dao.NamedExec(sqlCreateRound, rd)
	return err
}

const sqlChangeState2Bet = "update aviator_rounds set `start`=:start,`status`=:status,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *AviatorDao) ChangeToBetState(rd *aviatorp.AviatorRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2Bet, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeState2Run = "update aviator_rounds set `clientseeds`=:clientseeds,`h512`=:h512,`hhex`=:hhex,`hdecimal`=:hdecimal,`result`=:result,`status`=:status,`count`=:count,`runt`=:runt,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *AviatorDao) ChangeToRunState(rd *aviatorp.AviatorRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2Run, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeState2End = "update aviator_rounds set `end`=:end,`crashtime`=:crashtime,`cashouts`=:cashouts,`status`=:status,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *AviatorDao) ChangeToEndState(rd *aviatorp.AviatorRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2End, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlCreateAvtRoundAct = "insert into avt_play_acts(`id`,`dateline`,`roundid`,`uid`,`idx`,`op`,`result`,`payout`) values(:id,:dateline,:roundid,:uid,:idx,:op,:result,:payout)"

func (dao *AviatorDao) CreateAvtRoundAct(data *aviatorp.AvtRoundActEntity) error {
	_, err := dao.NamedExec(sqlCreateAvtRoundAct, data)
	return err
}

const sqlGetAlgoConfig = "select * from aviator_algo_conf limit 1"

func (dao *AviatorDao) GetAviatorAlgoConfig() (*aviatorp.AviatorAlgoConfig, error) {
	r := &aviatorp.AviatorAlgoConfig{}
	err := dao.Get(r, sqlGetAlgoConfig)
	return r, err
}

const sqlCreateAvtNodeRound = "insert into avt_node_rounds(`nodeid`,`instid`,`roundid`,`status`,`result`,`dateline`) values(:nodeid,:instid,:roundid,:status,:result,:dateline) on duplicate key update status=values(status),dateline=values(dateline)"

func (dao *AviatorDao) CreateAvtNodeRound(data *aviatorp.AvtNodeRoundEntity) error {
	_, err := dao.NamedExec(sqlCreateAvtNodeRound, data)
	return err
}

const sqlGetAvtNodeRound = "select * from avt_node_rounds where `nodeid`=? and `instid`=? and `roundid`=? limit 1"

func (dao *AviatorDao) GetAvtNodeRound(nodeId, instId, roundId int64) (*aviatorp.AvtNodeRoundEntity, error) {
	r := &aviatorp.AvtNodeRoundEntity{}
	err := dao.Get(r, sqlGetAvtNodeRound, nodeId, instId, roundId)
	return r, err
}
